const express = require('express')
const bijimodle = require('../model/bijimodle')
let router = express.Router()
const session = require('express-session')
let loginvertify = (req, res,next) => {
    console.log(req.session);
    if (req.session) {
        if (req.session.name) {
            next()
        } else {
            res.render('login.html')
        }
    } else {
        res.render('login.html')
    }
}
router.get('/', (req, res) => {
    let session = req.session || null
    bijimodle.find((err, data) => {
        res.render('index.html', { data,session })
    })
})
router.get('/add',loginvertify,(req, res) => {
    res.render('add.html')
})
router.get('/edit',loginvertify, (req, res) => {
    let id = req.query.id
    bijimodle.findOne({ _id: id }, (err, data) => {
        res.render('edit.html', { data: data })
    })
})
router.get('/delete',loginvertify, (req, res) => {
    let id = req.query.id
    bijimodle.deleteOne({ _id: id }, (err, data) => {
        console.log('删除成功');
    })
    res.render('jump.html', { msg: '删除' })
})
router.get('/getSession', (req, res) => {
    res.send('pagerouter.js中的session中name的值为' + req.session.name)
})
router.get('/login',(req,res)=>{
    res.render('login.html')
})
router.get('/register',(req,res)=>{
    res.render('register.html',{msgg:null})
})
router.get('/logout',(req,res)=>{
    req.session.destroy()
    res.render('jump.html',{msg:'退出'})
})
router.get('/center',(req,res)=>{
    res.render('center.html')
})
router.get('/detail/:id',(req,res)=>{
    let {id} = req.params
    console.log(id);
    let data = bijimodle.findOne({_id:id},(err,data)=>{
        res.render('detail.html',{data})
    })
})
module.exports = router